﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace REVISIONMAX30
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        public EventManager EventManager = null;
        public UIManager UIManager = null;
        public DataObjects.Data Data = null;

        private void frmMain_Load(object sender, EventArgs e)
        {

            EventManager = new EventManager();
            UIManager = new UIManager(mDockPanel, EventManager);

            UIManager.EventManager.UpdateProgress += new FrameWork.WinUI.EventManagerBase.DelegateUpdateProgress(EventManager_UpdateProgress);

            UIManager.InitResultPanel();
            UIManager.InitGroupPanel();

            //UIManager.GroupListPanel.DockTo(UIManager.TitleListPanel.Pane, DockStyle.Fill, -1);
            //UIManager.TitleListPanel.Activate();
            //UIManager.EventManager.AddTitleItem += new EventManager.DelegateAddTitleItem(UIManager.TitleListPanel.AddOrUpdateItem);

            Data = DataObjects.Data.Load(UIManager.EventManager);

            UIManager.InitData(Data);


        }

        private void btnShowHideGroups_Click(object sender, EventArgs e)
        {
            btnShowHideGroups.Text = UIManager.ShowHideGroups();
        }

        private void btnShowHideResults_Click(object sender, EventArgs e)
        {
            btnShowHideResults.Text = UIManager.ShowHideResults();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Data.Save(UIManager.EventManager);
        }

        public SortedList<Guid, FrameWork.WinUI.Controls.StatusStripProgress> mStatusStripProgressList = new SortedList<Guid, FrameWork.WinUI.Controls.StatusStripProgress>();
        public void EventManager_UpdateProgress(Guid pKey, int pTotal, int pCurrent, string pDescription)
        {
            if (InvokeRequired)
            {
                this.Invoke(new FrameWork.WinUI.EventManagerBase.DelegateUpdateProgress(EventManager_UpdateProgress), pKey, pTotal, pCurrent, pDescription);
            }
            else
            {
                FrameWork.WinUI.Controls.StatusStripProgress tStatusStripProgress = null;

                if (mStatusStripProgressList.ContainsKey(pKey))
                { tStatusStripProgress = mStatusStripProgressList[pKey]; }
                else
                {
                    tStatusStripProgress = new FrameWork.WinUI.Controls.StatusStripProgress()
                    {
                        Key = pKey,
                        ToolStripLabel = new ToolStripLabel(pDescription),
                        ToolStripProgressBar = new ToolStripProgressBar()
                    };

                    mStatusBar.Items.Add(tStatusStripProgress.ToolStripLabel);
                    mStatusBar.Items.Add(tStatusStripProgress.ToolStripProgressBar);

                    mStatusStripProgressList.Add(pKey, tStatusStripProgress);
                }

                if ((pCurrent == 0 && pTotal == 0) || pCurrent == pTotal)
                {
                    mStatusBar.Items.Remove(tStatusStripProgress.ToolStripLabel);
                    mStatusBar.Items.Remove(tStatusStripProgress.ToolStripProgressBar);
                    mStatusStripProgressList.Remove(pKey);
                }
                else
                {
                    tStatusStripProgress.ToolStripProgressBar.Maximum = pTotal;
                    tStatusStripProgress.ToolStripProgressBar.Value = pCurrent % pTotal;
                    tStatusStripProgress.ToolStripLabel.Text = pDescription;
                }
            }
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Save data?", "Save data?", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                this.Data.Save(UIManager.EventManager);
        }


    }
}
